﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using TMDT_Nhom1A.DTO;
using System.Collections.Generic;

namespace TMDT_Nhom1A.DAO
{
    public class BankWebservice_DAO
    {
        private static QL_DoanhNghiepDataContext DBcontext = new QL_DoanhNghiepDataContext();

        public static List<BANK_WEBSERVICE> getAllBankWebservice()
        {
            
            try
            {
                //lay ra tat ca webservice ngan hang
                var result = from bank in DBcontext.BANK_WEBSERVICEs
                             select bank;

                return result.ToList();
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public static bool insertBankWebservice(BANK_WEBSERVICE bankWs)
        {
            try
            {
                bankWs.Active = true;
                DBcontext.BANK_WEBSERVICEs.InsertOnSubmit(bankWs);
                DBcontext.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public static bool updateBankWebservice(BANK_WEBSERVICE newBankWS)
        {
            try
            {
                BANK_WEBSERVICE bankWS = DBcontext.BANK_WEBSERVICEs.Single(b => b.Bank_WS_ID == newBankWS.Bank_WS_ID);

                bankWS.BankUrl = newBankWS.BankUrl;
                bankWS.BankWebservice_Url = newBankWS.BankWebservice_Url;
                bankWS.BankName = newBankWS.BankName;
                bankWS.Description = newBankWS.Description;

                DBcontext.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public static bool deleteBankWebservice(string bankWS_ID)
        {
            try
            {
                BANK_WEBSERVICE oldBank = (from bank in DBcontext.BANK_WEBSERVICEs
                                           where bank.Bank_WS_ID == bankWS_ID
                                           select bank).Single();
                var accountBankWsResult = from acc in DBcontext.AccountBankWebservices
                                          where acc.BankWS_ID == bankWS_ID
                                          select acc;

                if (accountBankWsResult.Count() >= 1)
                    DBcontext.AccountBankWebservices.DeleteAllOnSubmit(accountBankWsResult);

                DBcontext.BANK_WEBSERVICEs.DeleteOnSubmit(oldBank);
                DBcontext.SubmitChanges();

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}
